home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / mlib / draw / draw.cpp < prev    next >
C/C++ Source or Header  |  1994-04-01  |  8KB  |  241 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //     FILE: DRAW.CPP
  3. // Image editor application for GRAPH.LIB and IO.LIB
  4. /////////////////////////////////////////////////////////////////////////////
  5.  
  6. #include <mio.h>
  7. #include <mgr.h>
  8. #include <mmulle.h>
  9. #include <mevent.h>
  10. #include <mwin.h>
  11. #include <mparent.h>
  12. #include <mcontrol.h>
  13. #include "draw.h"
  14.  
  15. /*--------------------------------------*/
  16.  RECT CanvasArea    (6,21,250,150);
  17.  RECT ColorsArea    (5,155,165,195);
  18.  RECT HeaderArea    (5,15,250,150);
  19.  RECT NewButton     (255,20,315,30);
  20.  RECT LoadButton    (255,35,315,45);
  21.  RECT SaveButton    (255,50,315,60);
  22.  RECT SizeButton    (255,65,315,75);
  23.  RECT QuitButton    (255,80,315,90);
  24.  RECT DrawingButton (255,95,315,105);
  25.  RECT FillButton    (255,110,315,120);
  26.  RECT EraseButton   (255,125,315,135);
  27.  RECT MagnifyButton (255,140,315,150);
  28.  RECT ScrollUButton (275,155,295,174);
  29.  RECT ScrollDButton (275,176,295,195);
  30.  RECT ScrollRButton (255,155,270,195);
  31.  RECT ScrollLButton (300,155,315,195);
  32. /*--------------------------------------*/
  33.  
  34. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  35.  int               main ()
  36. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  37. {
  38.     initgraph();
  39.     Initialize();
  40.     root.Realize();
  41.     root.Run();
  42.     closegraph();
  43.     return (0);
  44. }
  45.  
  46. /********************************************/
  47.  void           Initialize (void)
  48. /********************************************/
  49. {
  50. MULLE datafile;
  51. Canvas * DrawSpace;
  52. MButton * newButton;
  53. MCMap * colMap;
  54.  
  55.     DrawSpace = new Canvas;
  56.     newButton = new MButton (NewButton, "New", NEWB_HANDLE);
  57.     newButton->SetUserHandler (NewPixmap);
  58.     newButton = new MButton (LoadButton, "Load", LOADB_HANDLE);
  59.     newButton->SetUserHandler (LoadPixmap);
  60.     newButton = new MButton (SaveButton, "Save", SAVEB_HANDLE);
  61.     newButton->SetUserHandler (SavePixmap);
  62.     newButton = new MButton (SizeButton, "Resize", SIZEB_HANDLE);
  63.     newButton->SetUserHandler (ResizePixmap);
  64.     newButton = new MButton (QuitButton, "Quit", QUITB_HANDLE);
  65.     newButton->SetUserHandler (QuitApp);
  66.     newButton = new MButton (DrawingButton, "Draw", DRAWB_HANDLE);
  67.     newButton->SetUserHandler (LoadDraw);
  68.     newButton = new MButton (FillButton, "Fill", FILLB_HANDLE);
  69.     newButton->SetUserHandler (LoadFill);
  70.     newButton = new MButton (EraseButton, "Erase", ERASEB_HANDLE);
  71.     newButton->SetUserHandler (LoadErase);
  72.     newButton = new MButton (MagnifyButton, "Magnify", MAGNIFYB_HANDLE);
  73.     newButton->SetUserHandler (LoadMagnify);
  74.     newButton = new MButton (ScrollUButton, "/\\", SCROLLBU_HANDLE);
  75.     newButton->SetUserHandler (ScrollPixmapUp);
  76.     newButton = new MButton (ScrollDButton, "\\/", SCROLLBD_HANDLE);
  77.     newButton->SetUserHandler (ScrollPixmapDown);
  78.     newButton = new MButton (ScrollRButton, "<", SCROLLBL_HANDLE);
  79.     newButton->SetUserHandler (ScrollPixmapLeft);
  80.     newButton = new MButton (ScrollLButton, ">", SCROLLBR_HANDLE);
  81.     newButton->SetUserHandler (ScrollPixmapRight);
  82.     colMap = new MCMap (ColorsArea, 5, 5, COLORS_HANDLE);
  83.     FinishSetup (HROOT);
  84.  
  85.     datafile.SetMainFile ("draw.dat");
  86.     mouse.Load ("draw.dat", datafile.GetOffset ("arrow.cur"));
  87.     gr.font.Load ("draw.dat", datafile.GetOffset ("default.fnt"));
  88. }
  89.  
  90. /********************************************/
  91.        Canvas :: Canvas (void)
  92. /********************************************/
  93. : MScGrid (CanvasArea, 1, 1, CANVAS_HANDLE)
  94. {
  95.     memset (color, 0x00, N_COLOR_TYPES);
  96.     pixmap = new IMAGE (MAXX, MAXY, NULL);
  97.     pixmap->Clear();
  98.     filename.Resize (13);
  99.     filename.Clear();
  100.     ResizeWorld (MAXX, MAXY);
  101.     mode = DRAWING;
  102. }
  103.  
  104. /********************************************/
  105.  WORD Canvas :: Handler
  106. /********************************************/
  107. (EVENT event)
  108. {
  109. static int oldx,oldy;
  110. int status = EVENT_NOT_USED;
  111.  
  112.     if ((event->button & leftButton) && (IsInside (box, event->xpos, event->ypos)))
  113.     {
  114.        mouse.Hide();
  115.        push_DC();
  116.        setviewport (box);
  117.  
  118.        // First draw on screen
  119.        if (CellLength == 1)
  120.        {
  121.       switch (mode){
  122.          case DRAWING:
  123.           if (oldx != event->xpos || oldy != event->ypos)
  124.              line (oldx,oldy, event->xpos,event->ypos);
  125.           else
  126.              putpixel (event->xpos,event->ypos);
  127.           break;
  128.          case FILLING:
  129.           floodfill (event->xpos, event->ypos);
  130.           break;
  131.          case ERASING:
  132.           bar (event->xpos, event->ypos, event->xpos + 10, event->ypos + 10);
  133.           break;
  134.          case MAGNIFYING:
  135.           if (CellLength == 1)
  136.           {
  137.              SetCellSize (5,5);
  138.              SetSpeed (5);
  139.           }
  140.           CreateEvent (REFRESH_WINDOW, 0, NULL, 0L, CANVAS_HANDLE);
  141.           break;
  142.       }
  143.  
  144.       // Now draw to pixmap
  145.  
  146.       setdpy (pixmap->GetBitmap(), pixmap->GetLength(), pixmap->GetWidth());
  147.       switch (mode){
  148.          case DRAWING:
  149.           if (oldx != event->xpos || oldy != event->ypos)
  150.              line (oldx - box.left + vxpos,
  151.                oldy - box.top + vypos,
  152.                event->xpos - box.left + vxpos,
  153.                event->ypos - box.top + vypos);
  154.           else
  155.              putpixel (event->xpos - box.left + vxpos,
  156.                    event->ypos - box.top + vypos);
  157.           break;
  158.          case FILLING:
  159.           floodfill ((event->xpos - box.left) + vxpos,
  160.                  (event->ypos - box.top) + vypos);
  161.           break;
  162.          case ERASING:
  163.           bar (event->xpos - box.left + vxpos,
  164.                event->ypos - box.top + vypos,
  165.                event->xpos - box.left + vxpos + 10,
  166.                event->ypos - box.top + vypos + 10);
  167.           break;
  168.       }
  169.        }
  170.        else
  171.        {
  172.       switch (mode){
  173.          case DRAWING:
  174.           push_DC();
  175.           setdpy (pixmap->GetBitmap(), pixmap->GetLength(), pixmap->GetWidth());
  176.           putpixel ((event->xpos - box.left + vxpos) / CellLength,
  177.                 (event->ypos - box.top + vypos) / CellWidth);
  178.           pop_DC();
  179.           Draw ((event->xpos - box.left + vxpos) / CellLength,
  180.             (event->ypos - box.top + vypos) / CellWidth);
  181.           break;
  182.          case MAGNIFYING:
  183.           SetCellSize (1,1);
  184.           SetSpeed (3);
  185.           CreateEvent (REFRESH_WINDOW, 0, NULL, 0L, CANVAS_HANDLE);
  186.           break;
  187.       }
  188.        }
  189.  
  190.        pop_DC();
  191.        mouse.RescanBackgr();
  192.        mouse.Show();
  193.        status = EVENT_USED;
  194.     }
  195.     else
  196.        status = MWindow :: Handler (event);
  197.  
  198.     oldx = event->xpos;
  199.     oldy = event->ypos;
  200.  
  201.     return (status);
  202. }
  203.  
  204. /********************************************/
  205.  void Canvas :: Draw
  206. /********************************************/
  207. (int cx, int cy)
  208. {
  209.     setfillstyle (SOLID_FILL, *(BYTE*)(pixmap->GetBitmap() + (cy * pixmap->GetLength() + cx)));
  210.     bar (box.left - vxpos + cx * CellLength,
  211.      box.top - vypos + cy * CellWidth,
  212.      box.left - vxpos + (cx + 1) * CellLength - 1,
  213.      box.top - vypos + (cy + 1) * CellWidth - 1);
  214. }
  215.  
  216. /********************************************/
  217.  void Canvas :: Draw (void)
  218. /********************************************/
  219. {
  220. POINT TopLeft;
  221. int x,y;
  222.     mouse.Hide();
  223.     push_DC();
  224.     setviewport (box);
  225.     if (CellLength == 1 && CellWidth == 1)
  226.        pixmap->Put (box.left - vxpos, box.top - vypos);
  227.     else
  228.     {
  229.        TopLeftCell (TopLeft);
  230.        for (y = 0; y < GetVisibleWidth(); ++ y)
  231.       for (x = 0; x < GetVisibleLength(); ++ x)
  232.              Draw (x + TopLeft.x, y + TopLeft.y);
  233.     }
  234.     setcolor (15);
  235.     setviewport (box.left - 2, box.top - 2, box.right + 2, box.bottom + 2);
  236.     rectangle (box.left - 1, box.top - 1, box.right, box.bottom);
  237.     pop_DC();
  238.     mouse.RescanBackgr();
  239.     mouse.Show();
  240. }
  241.